﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wizard.UI;
using System.IO;
using System.Text.RegularExpressions;

namespace DVRMS_Renamer
{
    public partial class wizPage7 : Wizard.UI.InternalWizardPage
    {
        private frmMain mobjOwnerForm;

        public wizPage7(frmMain OwnerForm)
        {
            InitializeComponent();

            mobjOwnerForm = OwnerForm;
        }

        private void wizPage7_WizardPageSetActive(object sender, CancelEventArgs e)
        {
            SetWizardButtons(WizardButtons.Next);
        }

        private void wizPage7_WizardNext(object sender, WizardPageEventArgs e)
        {
            string strSource = "";
            string strDestination = "";
            
            if (Globals.selectedFilesToRename.Count > 0)
            {
                foreach (File theFile in Globals.selectedFilesToRename)
                {
                    strSource = theFile.sourcePath + theFile.originalFileName;
                    strDestination = theFile.sourcePath + theFile.newFilename;

                    System.IO.File.Move(strSource, strDestination);
                }
            }

            if (Globals.selectedFilesToDelete.Count > 0)
            {
                foreach (File theFile in Globals.selectedFilesToDelete)
                {
                    System.IO.File.Delete(theFile.sourcePath + theFile.originalFileName);
                }
            }
        }

        private void wizPage7_WizardBack(object sender, Wizard.UI.WizardPageEventArgs e)
        {
            e.NewPage = "wizPage5";
        }

        private void wizPage7_Load(object sender, EventArgs e)
        {
            string strReview = "";

            // TODO:DS Remove files to be deleted from the rename collection
            //         Also, remove any deletes from the rename collection
            if (Globals.selectedFilesToDelete.Count > 0 && Globals.selectedFilesToRename.Count > 0)
            {
                foreach (File theDeleteFile in Globals.selectedFilesToDelete)
                {
                    foreach (File theRenameFile in Globals.selectedFilesToRename)
                    {
                        if (theRenameFile.originalFileName == theDeleteFile.originalFileName)
                        {
                            theRenameFile.remove = true;
                        }
                    }
                }

                FileList selectedFiles = new FileList();
                selectedFiles.AddRange(Globals.selectedFilesToRename.Clone());

                Globals.selectedFilesToRename.Clear();

                foreach (File theFile in selectedFiles)
                {
                    if (!theFile.remove) Globals.selectedFilesToRename.Add(theFile);
                }
            }

            if (Globals.selectedFilesToRename.Count > 0)
            {
                strReview = "Rename the following files:\r\n";

                foreach (File theFile in Globals.selectedFilesToRename)
                {
                    strReview += "    \"" + theFile.originalFileName + "\"  ->  \"" + theFile.newFilename + "\"\r\n";
                }
            }

            if (Globals.selectedFilesToDelete.Count > 0)
            {
                if (strReview.Trim().Length > 0) strReview += "\r\n";
                strReview += "Delete the following duplicate files:\r\n";

                foreach (File theFile in Globals.selectedFilesToDelete)
                {
                    strReview += "    \"" + theFile.originalFileName + "\"\r\n";
                }
            }

            txtReview.Text = strReview;
        }
    }
}
